<template>
    <a-spin :spinning="confirmLoading">
      <j-form-container :disabled="formDisabled">
        <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
          <a-row>
            <a-col :span="24">
              <a-form-model-item label="资料类别" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="documentType">
                <j-dict-select-tag type="list" v-model="model.documentType" dictCode="document _type" placeholder="请选择资料类别" />
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label="名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="documentName">
                <a-input v-model="model.documentName" placeholder="请输入名称"  ></a-input>
              </a-form-model-item>
            </a-col>

            <a-col :span="24">
                <a-form-model-item label="文件上传" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="fileList">
                    <j-upload v-model="fileList"></j-upload>
                </a-form-model-item>
              <!-- <a-form-model-item label="链接" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="documentUrl">
                <a-input v-model="model.documentUrl" placeholder="请输入链接"  ></a-input>
              </a-form-model-item> -->
            </a-col>
            <a-col :span="24">
              <a-form-model-item label="值" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="documentValue">
                <a-input v-model="model.documentValue" placeholder="请输入值"  ></a-input>
              </a-form-model-item>
            </a-col>
          </a-row>
        </a-form-model>
      </j-form-container>
    </a-spin>
  </template>
  
  <script>
  
    import { httpAction, getAction } from '@/api/manage'
    import { validateDuplicateValue } from '@/utils/util'
  
    export default {
      name: 'YksEquipmentDocumentForm',
      components: {
      },
      props: {
        //表单禁用
        disabled: {
          type: Boolean,
          default: false,
          required: false
        }
      },
      data () {
        return {
          model:{
           },
          labelCol: {
            xs: { span: 24 },
            sm: { span: 5 },
          },
          wrapperCol: {
            xs: { span: 24 },
            sm: { span: 16 },
          },
          confirmLoading: false,
          validatorRules: {
             documentType: [
                { required: true, message: '请输入资料类别!'},
             ],
             documentName: [
                { required: true, message: '请输入名称!'},
             ],
          },
          url: {
            add: "/mng/yksEquipmentDocument/add",
            edit: "/mng/yksEquipmentDocument/edit",
            queryById: "/mng/yksEquipmentDocument/queryById"
          },
          fileList:[],
          currentEquipmentId:''
        }
      },
      computed: {
        formDisabled(){
          return this.disabled
        },
      },
      created () {
         //备份model原始值
        this.modelDefault = JSON.parse(JSON.stringify(this.model));
      },
      methods: {
        add () {
          this.edit(this.modelDefault);
        },
        edit (record) {
          this.model = Object.assign({}, record);
          
          let files=JSON.parse(record.documentUrl)
          for(var i=0;i<files.length;i++){
            this.fileList.push(files[i].key)
          }
          this.visible = true;
        },
        submitForm () {
          const that = this;
          
          // 触发表单验证
          this.$refs.form.validate(valid => {
            if (valid) {
              that.confirmLoading = true;
              let httpurl = '';
              let method = '';
              if(!this.model.id){
                httpurl+=this.url.add;
                method = 'post';
              }else{
                httpurl+=this.url.edit;
                 method = 'put';
              }
              if(this.fileList){
                let files=this.fileList.split(',')
                let filsobjs=[]
                for(var i=0;i<files.length;i++){
                  filsobjs.push({
                    key:files[i],
                    url:window._CONFIG['staticDomainURL'] +"/"+files[i]
                  })
                }
             this.model.documentUrl=JSON.stringify(filsobjs) 
              }
              this.model.equipmentId=this.currentEquipmentId
              httpAction(httpurl,this.model,method).then((res)=>{
                if(res.success){
                  that.$message.success(res.message);
                  that.$emit('ok');
                }else{
                  that.$message.warning(res.message);
                }
              }).finally(() => {
                that.confirmLoading = false;
              })
            }
           
          })
        },
      }
    }
  </script>